﻿using CSRedis;
using Md.Demo.Entity;
using Md.Demo.Entity.MongoModels;
using Md.Demo.IBLL;
using Md.Demo.Service;
using Md.Demo.Tools;
using MongoDB.Driver;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Md.Demo.BLL
{
    public class InterviewApplication : IinterviewApplication
    {
        private readonly Logger _logger = LogManager.GetLogger("logger");
        private readonly ResponseMessage res;
        private readonly MongoContext _mongoContext;
        private readonly CSRedisClient _csRedis;
        public InterviewApplication(ResponseMessage responseMessage, MongoContext mongoContext, CSRedisClient csRedis)
        {
            res = responseMessage;
            _mongoContext = mongoContext;
            _csRedis = csRedis;
        }

        public Task<ResponseMessage> GetinterviewerList(GetListRequest request)
        {
            var result = _csRedis.Get<List<Interviewer>>("MD:Demo:Interviewer");
            if (result == null || !result.Any())
            {
                var interviewerCol = _mongoContext.Collection<Interviewer>();

                var query = interviewerCol.AsQueryable().Where(s => s.IsDelete == 0);

                result = query.ToList();

                _csRedis.Set("MD:Demo:Interviewer", result);

            }
            if (!string.IsNullOrWhiteSpace(request.KeyWord))
            {
                result = result.Where(s => s.Name.Contains(request.KeyWord)
                 || s.Mobile.Contains(request.KeyWord) || s.Email.Contains(request.KeyWord)).ToList();
            }
            res.data = result;

            return Task.FromResult(res);
        }

        public Task<ResponseMessage> GetDepatmentList(GetListRequest request)
        {
            var result = _csRedis.Get<List<Depatment>>("MD:Demo:Department");
            if (result == null || !result.Any())
            {
                var interviewerCol = _mongoContext.Collection<Depatment>();

                var query = interviewerCol.AsQueryable().Where(s => s.IsDelete == 0);

                result = query.ToList();

                _csRedis.Set("MD:Demo:Department", result);

            }
            if (!string.IsNullOrWhiteSpace(request.KeyWord))
            {
                result = result.Where(s => s.DepatmentName.Contains(request.KeyWord)).ToList();
            }
            res.data = result;

            return Task.FromResult(res);
        }
        public Task<ResponseMessage> GetJobList(JobListRequest request)
        {
            //throw new Exception("this is exception");

            var JobCol = _mongoContext.Collection<Job>();

            var query = JobCol.AsQueryable().Where(s => s.IsDelete == 0);

            if (request.Status != 0) query = query.Where(s => s.Status == (JobStatus)request.Status);

            if (!string.IsNullOrWhiteSpace(request.KeyWord))
                query = query.Where(s => s.JobName.Contains(request.KeyWord) ||
                s.JobDesc.Contains(request.KeyWord) || s.JobRequire.Contains(request.KeyWord));

            var count = query.Count();

            var result = query.OrderByDescending(s => s.CreateTime).GetPage(request.PageIndex, request.PageSize).ToList();
            res.data = new
            {
                count,
                result
            };
            return Task.FromResult(res);
        }

        public Task<ResponseMessage> GetStaffList(StaffListRequest request)
        {

            var StaffCol = _mongoContext.Collection<Staff>();

            var query = StaffCol.AsQueryable().Where(s => s.IsDelete == 0);

            if (request.Status != 0) query = query.Where(s => s.Progress == (ProgressEnum)request.Status);

            if (!string.IsNullOrWhiteSpace(request.KeyWord))
                query = query.Where(s => s.Name.Contains(request.KeyWord) ||
                s.Mobile.Contains(request.KeyWord) || s.Email.Contains(request.KeyWord));

            if (!string.IsNullOrWhiteSpace(request.JobId))
                query = query.Where(s => s.JobId == request.JobId);

            var count = query.Count();

            var result = query.OrderByDescending(s => s.CreateTime).GetPage(request.PageIndex, request.PageSize).ToList();
            res.data = new
            {
                count,
                result
            };
            return Task.FromResult(res);
        }

        public Task<ResponseMessage> GetInterviewList(InterviewListRequest request)
        {

            var interviewCol = _mongoContext.Collection<InterviewRecord>();

            var query = interviewCol.AsQueryable().Where(s => s.IsDelete == 0);

            if (request.Status != 0) query = query.Where(s => s.InterviewStatus == (InterviewStatusEunm)request.Status);

            if (!string.IsNullOrWhiteSpace(request.KeyWord))
                query = query.Where(s => s.StaffName.Contains(request.KeyWord));

            if (!string.IsNullOrWhiteSpace(request.StaffId))
                query = query.Where(s => s.StaffId == request.StaffId);

            var count = query.Count();

            var result = query.OrderByDescending(s => s.CreateTime).GetPage(request.PageIndex, request.PageSize).ToList();
            res.data = new
            {
                count,
                result
            };
            return Task.FromResult(res);
        }

        public Task<ResponseMessage> CreateJob(CreateJobRequest request)
        {
            _logger.Info(request);
            //var jobCol = _mongoContext.Collection<Job>();
            Job job = new Job
            {
                _id = request.Id,
                Department = request.Department,
                JobDesc = request.JobDesc,
                JobName = request.JobName,
                JobRequire = request.JobRequire,
                Status = (JobStatus)request.Status,
                UpdateTime = DateTime.Now
            };
            KafkaTest.Produce(job.ToJson());
            //  jobCol.UpsertOne(job);
            return Task.FromResult(res);
        }

        public Task<ResponseMessage> DeleteJob(DeleteRequest request)
        {
            var jobCol = _mongoContext.Collection<Job>();
            var job = jobCol.AsQueryable().FirstOrDefault(s => s._id == request.Id);
            job.IsDelete = 1;
            jobCol.UpsertOne(job);
            return Task.FromResult(res);
        }

        public Task<ResponseMessage> CreateStaff(CreateStaffRequest request)
        {
            var staffCol = _mongoContext.Collection<Staff>();
            Staff staff = new Staff
            {
                _id = request.Id,
                Name = request.Name,
                Birthday = request.Birthday.ToDateTime(),
                Email = request.Email,
                JobId = request.JobId,
                Mobile = request.Mobile,
                Progress = (ProgressEnum)request.Progress,
                ResumeSourse = (ResumeSourseEnum)request.ResumeSourse,
                ResumeUrl = request.ResumeUrl,
                Sex = request.Sex,
                UpdateTime = DateTime.Now
            };
            staffCol.UpsertOne(staff);
            return Task.FromResult(res);
        }

        public Task<ResponseMessage> DeleteStaff(DeleteRequest request)
        {
            var staffCol = _mongoContext.Collection<Staff>();
            var staff = staffCol.AsQueryable().FirstOrDefault(s => s._id == request.Id);
            staff.IsDelete = 1;
            staffCol.UpsertOne(staff);
            return Task.FromResult(res);
        }

        public Task<ResponseMessage> CreateInterview(CreateInterviewRequest request)
        {
            var interviewCol = _mongoContext.Collection<InterviewRecord>();
            InterviewRecord interviewRecord = new InterviewRecord
            {
                _id = request.Id,
                Appraise = request.Appraise,
                InterviewerId = request.InterviewerId,
                InterviewStatus = (InterviewStatusEunm)request.InterviewStatus,
                InterviewTime = request.InterviewTime.ToDateTime(),
                JobId = request.JobId,
                Score = request.Score,
                StaffId = request.StaffId,
                StaffName = request.StaffName,
                UpdateTime = DateTime.Now
            };
            interviewCol.UpsertOne(interviewRecord);
            return Task.FromResult(res);
        }

        public Task<ResponseMessage> DeleteInterview(DeleteRequest request)
        {
            var interviewCol = _mongoContext.Collection<InterviewRecord>();
            var interviewRecord = interviewCol.AsQueryable().FirstOrDefault(s => s._id == request.Id);
            interviewRecord.IsDelete = 1;
            interviewCol.UpsertOne(interviewRecord);
            return Task.FromResult(res);
        }
    }
}
